package desktop.lan.remote.util;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.server.ExportException;
import java.util.logging.Level;
import java.util.logging.Logger;

import desktop.lan.LanInfo;
import desktop.lan.cooperate.LanCooperateClient;

public class RemoteServer<I extends Remote, T extends I> {

	public RemoteServer(T remoteImpl, String remoteName) {

		RmiServiceConf.runService();

		try {
			I remoteInterface = remoteImpl;
			Naming
					.rebind("rmi://" + LanInfo.getSelfIp() + ":"
							+ LanInfo.getSelfPort() + "/" + remoteName,
							remoteInterface);
		} catch (MalformedURLException ex) {
			Logger.getLogger(LanCooperateClient.class.getName()).log(
					Level.SEVERE, null, ex);
		} catch (ExportException ex) {
			System.out.println("Can't open two games in a time!");
			System.exit(0);
		} catch (RemoteException ex) {
			Logger.getLogger(LanCooperateClient.class.getName()).log(
					Level.SEVERE, null, ex);
		}
	}

}